package com.demo.chapter11future;

import java.util.concurrent.CompletableFuture;

//组合多个CompletableFuture
public class ThenCompose {

	public static Integer calc(Integer para){
		try{
			//模拟一个长时间执行
			Thread.sleep(1000);
		}catch(InterruptedException e){
			e.printStackTrace();
		}
		return para / 2;
	}

	public static void main(String[] args) throws Exception {
		final CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> calc(50))
			//一个CompletableFuture可以在执行完成后，将执行结果通过Future传递给下一个CompletionStage进行处理（Future接口返回新的CompletionStage实例）
			.thenCompose(i -> CompletableFuture.supplyAsync(() -> calc(i)))
			.thenApply(i -> Integer.toString(i))
			.thenApply(str -> "\"" + str + "\"")
			.thenAccept(System.out::println);
		future.get();
	}

}
